package com.daoImpl;

import java.util.List;



import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.UserDao;
import com.entity.User;

@Repository 
public class UserDaoImpl implements UserDao{
    @Autowired     
    private SessionFactory sessionFactory;
    

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void delete(int id) {
        User user = (User)sessionFactory.getCurrentSession().get(User.class,id);
        sessionFactory.getCurrentSession().delete(user);
    }
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void deleteByUid(String uid){
    	 User user=(User)findByUid(uid);
    	 if (null==user) return;
    	 sessionFactory.getCurrentSession().delete(user);
    }
    
    
    
    

    /*@Override
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
    public List<Object> findAll() {
        List list = sessionFactory.getCurrentSession().createQuery("from User").list();
        return list;
    }*/

    @Override
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
    public Object findByld(int id) {
        User user=sessionFactory.getCurrentSession().get(User.class, id);
        return user;
    }
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
    public Object findByUid(String uid){
    	List list = sessionFactory.getCurrentSession().createQuery("from User u where u.uid = :uid").setParameter("uid", uid).list();
    	if(list.isEmpty())return null;
    	return list.get(0);
    }
    
    

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void insert(Object o) {
        sessionFactory.getCurrentSession().save(o);

    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void update(Object o) {
        sessionFactory.getCurrentSession().update(o);

    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
    public List findByNamePwd(String uid, String pwd) {
        
    	List list = sessionFactory.getCurrentSession().createQuery("from User u where u.uid = :uid AND u.password = :password").setParameter("uid", uid).setParameter("password", pwd).list();
        return list;
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
    public List findByName(String name) {
        List list = sessionFactory.getCurrentSession().createQuery("from User u where u.name = :name").setParameter("name", name).list();       
        return list;
    }

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public boolean IsExistUid(String uid) {
		List list = sessionFactory.getCurrentSession().createQuery("from User u where u.uid = :uid").setParameter("uid", uid).list();
        if(!list.isEmpty())return true;       
        return false;
	}
    
   
	
	
	
	
	
	
	
}
